//
//  LSHTTPManager.m
//  ZLFastDoctor
//
//  Created by 李宗帅 on 16/5/21.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "LSHTTPManager.h"
#import "YYModel.h"
#import "MOConcernModel.h"

//定义一个请求数据的相同网址,用来拼接
#define LSBaseURL [NSURL URLWithString:@"http://iosapi.itcast.cn/"]
                                         //http://iosapi.itcast.cn/doctor/matchedDoctorCount.json.php
                                         //http://iosapi.itcast.cn/matchedDoctorCount.json.php
@implementation LSHTTPManager

+ (instancetype)sharedManager {
    
    static dispatch_once_t onceToken;
    
    static LSHTTPManager *Manager;
    
    dispatch_once(&onceToken, ^{
        
        Manager = [[self alloc]initWithBaseURL:LSBaseURL];
        Manager.requestSerializer =   [AFJSONRequestSerializer serializer];
        
        // 数据类型解析失败,这个错误工作中经常会见到
        // 设置更多的数据类型
        Manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript" ,@"text/html", nil];
        
    });
    
    return Manager;
}


- (void)GET:(NSString *)path parameters:(id)parameters comletionHandle:(void(^)(id responseObject, NSError *error))comletionHandle {
    [self GET:path parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        comletionHandle(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        comletionHandle(nil,error);
    }];
}


- (void)POST:(NSString *)path parameters:(id)parameters comletionHandle:(void(^)(id responseObject,NSError *error))comletionHandle {
    [self POST:path parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        comletionHandle(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        comletionHandle(nil,error);
    }];
}

- (void)requestWithType:(LSHTTPManagerType)type URLString:(NSString *)URLString parameters:(id)parameters success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nonnull, NSError * _Nullable))failure{
    [self requestWithType:type URLString:URLString parameters:parameters progress:nil success:success failure:failure];
}
//请求天气数据
- (void)requestWithType:(LSHTTPManagerType)type URLString:(NSString *)URLString parameters:(id)parameters progress:(void (^)(NSProgress * _Nonnull))downloadProgress success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success failure:(void (^)(NSURLSessionDataTask * _Nonnull, NSError * _Nullable))failure{
    if (type == LSHTTPManagerTypeGet) {
        [self GET:URLString parameters:parameters progress:downloadProgress success:success failure:failure];
    }
    
    if (type == LSHTTPManagerTypePost) {
        [self POST:URLString parameters:parameters progress:downloadProgress success:success failure:failure];
    }
}
- (void)concernDoctor:(void(^)(NSArray * arr1))block{
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    
    NSDictionary *dic = @{
                          @"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089,@"page":@1,@"has_diagnosis":@2
                          };
    
    [self POST:url parameters:dic comletionHandle:^(id responseObject, NSError *error) {
        
        if (responseObject) {
            
            NSArray *arr = responseObject[@"data"];
            self.model = [NSArray yy_modelArrayWithClass:[MOConcernModel class] json:arr];
            
            block(self.model);
        }
        
    }];
}
    
    

- (NSArray *)model {
    
    if (_model == nil) {
        _model = [NSArray array];
    }
    return _model;
}

@end
